package Javatest;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Random;
import java.util.stream.IntStream;

public class test_10_9 {
    public static void main(String[] args) {
        test1();
        test2();
        test3();
        test4();
        test5();
        test6();
        test7();
    }


    public static void test1() {
        Random rand = new Random();
        int number = (int) IntStream.generate(()->rand.nextInt(100))
                .limit(30)
                .filter(n -> n % 2 == 0)
                .count();
        System.out.println("偶数的个数为"+number);
    }


    public static void test2() {
        String[] strings = {"Hello","Morning","afternoon","boY","girls"};
        String[] upperstrings = Arrays.stream(strings)
                .map(String::toUpperCase)
                .toArray(String[]::new);
        for(String s : upperstrings){
            System.out.println(s);
        }
    }

    public static void test3() {
        IntStream.range(1,100+1)
                .filter(n -> (n % 7) == 0)
                .forEach(n -> System.out.println(n));
    }

    public static void test4() {
        String[] strings = {"Java","Python","Learning","Happy"};
        String[] sortedstrings = Arrays.stream(strings)
                .sorted(Comparator.comparing(s->(int) s.chars().filter(ch->"aeiouAEIOU".indexOf(ch)>=0).count()))
                .toArray(String[]::new);
        for(String s : sortedstrings){
            System.out.println(s);
        }
    }

    public static void test5() {
        Integer[] numbers = {1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,1,2,3,4,5,6,7,8,9};
        Integer[] uniquenumbers = Arrays.stream(numbers)
                .distinct()
                .toArray(Integer[]::new);
        for(Integer num:uniquenumbers)
            System.out.println(num);
    }

    public static void test6() {
        Integer[] numbers = {1,3,5,7,9,11,13,15,17,19,22,23,24,26,29};
        Integer[] primenumbers = Arrays.stream(numbers)
                .filter(test_10_9::isPrime)
                .toArray(Integer[]::new);
        for(Integer num:primenumbers)
            System.out.println(num);
    }

    public static void test7() {
        Random rand = new Random();
        int Max = IntStream.generate(()-> rand.nextInt(51)+50)
                .limit(15)
                .max()
                .orElse(-1);
        System.out.println("最大值为"+Max);
    }

    public static boolean isPrime(int n){
        if(n<=1)
            return false;
        for(int i = 2;i<n;i++){
            if(n%i==0)
                return false;
        }
        return true;
    }

}
